package com.nashuju.web.controller.article;

import org.durcframework.core.DefaultGridResult;
import org.durcframework.core.GridResult;
import org.durcframework.core.MessageResult;
import org.durcframework.core.controller.CrudController;
import org.durcframework.core.expression.ExpressionQuery;
import org.durcframework.core.expression.subexpression.ValueExpression;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nashuju.web.entity.article.ArticleComment;
import com.nashuju.web.entity.article.ArticleCommentSch;
import com.nashuju.web.service.article.ArticleCommentService;

@Controller
public class ArticleCommentController extends CrudController<ArticleComment, ArticleCommentService> {

    @RequestMapping("/addCArticleComment.do")
    public @ResponseBody MessageResult addCArticleComment(ArticleComment entity) {

        return this.save(entity);
    }

    @RequestMapping("/listCArticleComment.do")
    public @ResponseBody GridResult listCArticleComment(ArticleCommentSch searchEntity) {
        return this.query(searchEntity);
    }



    @RequestMapping("/listCArticleCommentTree.do")
    public @ResponseBody Object listCArticleCommentTree(ArticleCommentSch searchEntity) {

        /*
         * 默认EasyUI树形分页列表式
         */
        if (searchEntity.getParentIdSch() == 0) {
            DefaultGridResult resultGrid = (DefaultGridResult) this.query(searchEntity);

            for (int i = 0; i < resultGrid.getRows().size(); i++) {
                ArticleComment getRow = (ArticleComment) resultGrid.getRows().get(i);
                ExpressionQuery queryCount = new ExpressionQuery();
                queryCount.add(new ValueExpression("parent_id", getRow.getId()));
                if (this.getService().findTotalCount(queryCount) > 0) {
                    getRow.setState("closed");
                } else {
                    getRow.setState("open");
                }
            }

            return resultGrid;
        } else {
            /*
             * 子类节点式
             */
            ArticleCommentSch entity = new ArticleCommentSch();
            entity.setAbIdSch(searchEntity.getAbIdSch());
            entity.setParentIdSch(searchEntity.getParentIdSch());
            entity.setIdSch(searchEntity.getIdSch());
            DefaultGridResult resultGrid = (DefaultGridResult) this.query(entity);

            for (int i = 0; i < resultGrid.getRows().size(); i++) {
                ArticleComment getRow = (ArticleComment) resultGrid.getRows().get(i);
                ExpressionQuery queryCount = new ExpressionQuery();
                queryCount.add(new ValueExpression("parent_id", getRow.getId()));
                if (this.getService().findTotalCount(queryCount) > 0) {
                    getRow.setState("closed");
                } else {
                    getRow.setState("open");
                }
            }

            return resultGrid.getRows();
        }

    }

    @RequestMapping("/updateCArticleComment.do")
    public @ResponseBody MessageResult updateCArticleComment(ArticleComment entity) {
        return this.update(entity);
    }

    @RequestMapping("/delCArticleComment.do")
    public @ResponseBody MessageResult delCArticleComment(ArticleComment entity) {
        return this.delete(entity);
    }

    @RequestMapping("/getCArticleCommentById.do")
    public @ResponseBody ArticleComment getCArticleCommentById(ArticleComment entity) {
        return this.getService().get(entity.getId());
    }

}
